/**
 * 
 */
package com.sunwayhorizo.health.common;

import java.io.ByteArrayOutputStream;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

/**
 * =============================================================
 * @ClassName StreamUtils <br>
 * @PackageName com.sunwayhorizo.health.hl7.common <br>
 * @Description 流工具，用来将输入流转换成byte数组或者char数组<br>
 * @Author jinzw <br>
 * @Date 2010-11-1 <br>
 * @Version 1.0 <br>
 * @ModifiedDesc <br>
 * @ModifiedDate <br>
 * @ModifiedBy <br>
 * @Email tanll@sunwaysoft.com.cn <br>
 * @Copyright 2010-2020 北京三维海容 All Rights Reserved. <br>
 * =============================================================
 */
public class StreamUtils {
	public static final int DEFAULT_CHUNK_SIZE = 1024;//默认缓冲大小
	
	/**
	 * 将inputStream写到字节数组中，用于处理Blob类型的数据
	 * @param stream 输入流
	 * @return 二进制数组
	 * @throws IOException
	 */
	public static byte [] inputStreamToByteArray(InputStream stream) throws IOException{
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		StreamUtils.copy(stream, buffer);
		return buffer.toByteArray();
	}
	
	/**
	 * 将inputStream写到字符数组中，用于处理Clob类型的数据
	 * @param stream 输入流
	 * @return
	 * @throws IOException
	 */
	public static char [] inputStreamToCharArray(InputStream stream) throws IOException{
		InputStreamReader reader = new InputStreamReader(stream);
		CharArrayWriter buffer = new CharArrayWriter();
		StreamUtils.copy(reader, buffer);
		return buffer.toCharArray();
	}

	/**
	 * 将字符读取流写到字符数组中，用于处理Clob类型的数据
	 * @param reader 字符读取流
	 * @return
	 * @throws IOException
	 */
	public static char [] readerToChartArray(Reader reader) throws IOException{
		CharArrayWriter buffer = new CharArrayWriter();
		StreamUtils.copy(reader, buffer);
		return buffer.toCharArray();
	}
	
	/**
	 * 将输入流拷贝到输出流
	 * @param inputStream 输入流
	 * @param outputStream 输出流
	 * @return
	 * @throws IOException
	 */
	public static long copy(InputStream inputStream, OutputStream outputStream) throws IOException {
		return copy( inputStream, outputStream, DEFAULT_CHUNK_SIZE );
	}

	/**
	 * 将输入流拷贝到输出流
	 * @param inputStream 输入流
	 * @param outputStream 输入流
	 * @param bufferSize 缓冲大小
	 * @return
	 * @throws IOException
	 */
	public static long copy(InputStream inputStream, OutputStream outputStream, int bufferSize) throws IOException {
		byte[] buffer = new byte[bufferSize];
		long count = 0;
		int n;
		while ( -1 != ( n = inputStream.read( buffer ) ) ) {
			outputStream.write( buffer, 0, n );
			count += n;
		}
		return count;

	}

	/**
	 * 将字符读取流拷贝到字符输出流
	 * @param reader 字符读取流
	 * @param writer 字符输出流
	 * @return
	 * @throws IOException
	 */
	public static long copy(Reader reader, Writer writer) throws IOException {
		return copy( reader, writer, DEFAULT_CHUNK_SIZE );
	}

	/**
	 * 将字符读取流拷贝到字符输出流
	 * @param reader 字符读取流
	 * @param writer 字符输出流
	 * @param bufferSize 缓冲大小
	 * @return
	 * @throws IOException
	 */
	public static long copy(Reader reader, Writer writer, int bufferSize) throws IOException {
		char[] buffer = new char[bufferSize];
		long count = 0;
		int n;
		while ( -1 != ( n = reader.read( buffer ) ) ) {
			writer.write( buffer, 0, n );
			count += n;
		}
		return count;

	}
}
